package com.library.relation.service;

import com.library.book.service.BookServiceImpl;
import com.library.entity.Book;
import com.library.entity.StaffRelation;
import com.library.entity.Staff;
import com.library.relation.dao.StaffRelationDaoImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional(readOnly=false)
public class StaffRelationServiceImpl {
	
	@Resource
	private StaffRelationDaoImpl staffRelationDao;
	@Resource
	private BookServiceImpl bookServiceImpl;

	public void addRelation(StaffRelation relation){
		this.staffRelationDao.saveRelation(relation);
	}

    @Transactional(readOnly=true)
	public StaffRelation getRelationById(Integer relationId)
	{
		return this.staffRelationDao.getRelationById(relationId);
	}

    @Transactional(readOnly=true)
	public List<StaffRelation> getStaffRelation(Staff s){
		return this.staffRelationDao.findByStaff(s);
	}

	@Transactional(readOnly=true)
	public StaffRelation getStaffRelation(Staff s1, Staff s2){
		return this.staffRelationDao.findByStaffAndStaff(s1,s2);
	}

	public void updateRelation(StaffRelation relation){
		this.staffRelationDao.updateRelation(relation);
	}

    @Transactional(readOnly=true)
	/*查找全部评价记录*/
	public List<StaffRelation> getAllRelation()
	{
		return this.staffRelationDao.getAll();
	}

	/*删除指定关系记录*/
	public void deleteRelation(int relationId){
		StaffRelation relation = this.getRelationById(relationId);
		this.staffRelationDao.deleteRelation(relation);
    }

    //删除staff相关的relation
    public void deleteRelationByStaff(Staff staff){
		List<StaffRelation> staffRelations = this.getStaffRelation(staff);
		Iterator<StaffRelation> iterator = staffRelations.iterator();
		StaffRelation staffRelation = null;

		while(iterator.hasNext()){
			staffRelation = iterator.next();
			this.staffRelationDao.deleteRelation(staffRelation);
		}
	}

    //基于图书更新员工之间的联系次数
    public void updateAllRelationByBooks(Staff staff,List<Book> bookList){
		List<Book> hotBooks = this.bookServiceImpl.getAllClassHotBook(5);	//热门图书
		StaffRelation relation = null;
		Set<Staff> staffSet;
		Staff s1 = null;
		Iterator<Staff> itOne = null;

		for(int i = 0;i < bookList.size();++i){
			staffSet = bookList.get(i).getStaffs();
			itOne = staffSet.iterator();

			while(itOne.hasNext()){
				s1 = itOne.next();

				if(s1.equals(staff)){		//同一员工 继续循环
					continue;
				}else{
					relation = this.getStaffRelation(s1,staff);
					if(relation != null){
						relation.setStaffOne(s1);
						relation.setStaffTwo(staff);
						relation.setCount(relation.getCount() + 1);

						this.staffRelationDao.updateRelation(relation);
					}else{
						relation = new StaffRelation();
						relation.setStaffOne(s1);
						relation.setStaffTwo(staff);
						relation.setCount(1);

						this.staffRelationDao.saveRelation(relation);
					}

					relation = this.getStaffRelation(staff,s1);
					if(relation != null){
						relation.setStaffOne(staff);
						relation.setStaffTwo(s1);
						relation.setCount(relation.getCount() + 1);

						this.staffRelationDao.updateRelation(relation);
					}else{
						relation = new StaffRelation();
						relation.setStaffOne(staff);
						relation.setStaffTwo(s1);
						relation.setCount(1);

						this.staffRelationDao.saveRelation(relation);
					}
				}
			}	//while
		}	//for
	}

	//基于目标员工与其他员工联系次数返回最密切的10员工 降序
	public Set<Staff> getRecommendationStaffByStaff(Staff staff){
		List<StaffRelation> relations = this.staffRelationDao.findByStaffOrderByCountDesc(staff);
		Set<Staff> staffs = new HashSet<Staff>();

		if(relations != null){
			for(int i = 0;i < relations.size() && staffs.size() <= 10;++i){
				staffs.add(relations.get(i).getStaffTwo());
			}
		}

		return staffs;
	}
}
